package com.java110.mall.shop.cmd.shop;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.area.AreaDto;
import com.java110.dto.distributionMode.DistributionModeDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.store.StoreDto;
import com.java110.intf.user.IAreaInnerServiceSMO;
import com.java110.intf.goods.IDistributionModeInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.user.IStoreV1InnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询待审核 商铺
 *
 * @return
 * @serviceCode /shop/queryShopsByAdmin
 * @path /app/shop/queryShopsByAdmin
 */
@Java110Cmd(serviceCode = "shop.queryShopsByAdmin")
public class QueryShopsByAdminCmd extends Cmd {

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IStoreV1InnerServiceSMO storeInnerServiceSMOImpl;

    @Autowired
    private IDistributionModeInnerServiceSMO distributionModeInnerServiceSMOImpl;

    @Autowired
    private IAreaInnerServiceSMO areaInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        super.validateAdmin(cmdDataFlowContext);

        //管理员查询
        if(reqJson.containsKey("storeId") && !StringUtil.isEmpty(reqJson.getString("storeId"))){
            StoreDto storeDto = new StoreDto();
            storeDto.setStoreId(reqJson.getString("storeId"));
            storeDto.setStoreTypeCd(StoreDto.STORE_TYPE_ADMIN);
            int count = storeInnerServiceSMOImpl.queryStoresCount(storeDto);
            if(count> 0){
                reqJson.remove("storeId");
            }
        }
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {

        ShopDto shopDto = BeanConvertUtil.covertBean(reqJson,ShopDto.class);
        shopDto.setStoreId(reqJson.getString("merchantId"));


//        if (StringUtil.isEmpty(shopDto.getState())) {
//            //shopDto.setStates(new String[]{ShopDto.STATE_Y, ShopDto.STATE_N});
//        }

        int count = shopInnerServiceSMOImpl.queryShopsCount(shopDto);

        List<ShopDto> shopDtos = null;
        if (count > 0) {
            shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
            List<String> shopIds = new ArrayList<>();
            for (ShopDto prod : shopDtos) {
                shopIds.add(prod.getShopId());
            }
            refreshDistributionMode(shopDtos, shopIds);

            freshShopImg(shopDtos);
            freshStore(shopDtos);
        } else {
            shopDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) shopDto.getRow()), count, shopDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        cmdDataFlowContext.setResponseEntity(responseEntity);
    }

    private void refreshDistributionMode(List<ShopDto> shopDtos, List<String> shopIds) {


        if (ListUtil.isNull(shopDtos) || ListUtil.isNull(shopIds)) {
            return;
        }
        // 刷入规格 值信息
        DistributionModeDto distributionModeDto = new DistributionModeDto();
        distributionModeDto.setShopIds(shopIds.toArray(new String[shopIds.size()]));
        List<DistributionModeDto> distributionModeDtos = distributionModeInnerServiceSMOImpl.queryDistributionModes(distributionModeDto);

        List<DistributionModeDto> tmpDistributionModeDtos = null;
        for (ShopDto tmpShopDto : shopDtos) {
            tmpDistributionModeDtos = new ArrayList<>();
            for (DistributionModeDto tmpDistributionModeDto : distributionModeDtos) {
                if (tmpShopDto.getShopId().equals(tmpDistributionModeDto.getShopId())) {
                    tmpDistributionModeDtos.add(tmpDistributionModeDto);
                }
            }
            if(ListUtil.isNull(tmpDistributionModeDtos)){
                DistributionModeDto  tmpDistributionModeDto = new DistributionModeDto();
                tmpDistributionModeDto.setModeId("-1");
                tmpDistributionModeDto.setModeName("-1");
                tmpDistributionModeDto.setFixedFee("0");
                tmpDistributionModeDtos.add(tmpDistributionModeDto);
            }
            tmpShopDto.setDistributionModeDtos(tmpDistributionModeDtos);
        }
    }

    /**
     * 刷入商户信息
     *
     * @param shopDtos
     */
    private void freshStore(List<ShopDto> shopDtos) {

        if (shopDtos == null || shopDtos.size() < 1) {
            return;
        }
        List<String> storeIds = new ArrayList<>();
        for (ShopDto shopDto : shopDtos) {
            storeIds.add(shopDto.getStoreId());
        }

        StoreDto storeDto = new StoreDto();
        storeDto.setStoreIds(storeIds.toArray(new String[storeIds.size()]));
        List<StoreDto> storeDtos = storeInnerServiceSMOImpl.queryStores(storeDto);

        for (ShopDto shopDto : shopDtos) {
            for (StoreDto tmpStoreDto : storeDtos) {
                if (shopDto.getStoreId().equals(tmpStoreDto.getStoreId())) {
                    shopDto.setStoreName(tmpStoreDto.getName());
                    shopDto.setStoreTel(tmpStoreDto.getTel());
                    shopDto.setStoreAddress(tmpStoreDto.getAddress());
                }
            }
        }
    }

    /**
     * @param shopDtos
     */
    private void freshShopImg(List<ShopDto> shopDtos) {
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
        String mallUrl = MappingCache.getValue(MappingConstant.URL_DOMAIN,"MALL_URL");

        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        mallUrl += (!StringUtil.isEmpty(mallUrl) && mallUrl.endsWith("/") ? "" : "/");

        for (ShopDto shopDto : shopDtos) {
            if(!StringUtil.isEmpty(shopDto.getShopLogo()) && !shopDto.getShopLogo().startsWith("http")) {
                shopDto.setShopLogo(imgUrl + shopDto.getShopLogo());
            }
            //shopDto.setShopLogo(imgUrl + shopDto.getShopLogo());
            shopDto.setPhoneIndexUrl(mallUrl + "#/pages/shopIndex/index?shopId=" + shopDto.getShopId());
            if (!StringUtil.isEmpty(shopDto.getAreaCode())) {
                AreaDto areaDto = new AreaDto();
                areaDto.setAreaCode(shopDto.getAreaCode());
                List<AreaDto> areaDtos = areaInnerServiceSMOImpl.getProvCityArea(areaDto);
                if (areaDtos != null && areaDtos.size() > 0) {
                    shopDto.setAreaName(areaDtos.get(0).getProvName() + "-" + areaDtos.get(0).getCityName() + "-" + areaDtos.get(0).getAreaName());
                }
            }
        }
    }
}
